import * as api from '../service/api'

import mock from './mock.data'


function mockdata (fun, cb) {
	console.log('mockdata');
	cb(mock[fun]);
}

export default ({ mock, fun, cb, err, final, requestType, ...data }) =>  {

	if(mock){
		mockdata(fun, cb);
		return;
	}

	const url = api[fun];

	//默认post请求。
	const method = requestType == 'get' ? 'GET' : 'POST';

	//把空属性去掉。
	Object.keys(data).forEach(key => {
		if(data[key] === undefined){
			delete data[key];
		}
	});

	data['company_code'] = 'LW00035';

	uni.request({
		url,
		data,
		method,
		header: {
			'content-type': 'application/json' // 默认值
		},
		success (res) {
			if(res.data.errcode == 0){
				if(cb) cb(res.data.data);
			}else{
				if(err) err(res.data.errmsg);
			}
		},
		fail (res) {
			console.log('统一请求 中的异常')
			console.log(res);
		},
		complete : final,
	});
}